package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.domain.Permission;
import com.woniuxy.domain.User;
import com.woniuxy.dto.DtoJSONResult;
import com.woniuxy.dto.DtoJSONResultCode;
import com.woniuxy.dto.DtoUserEnum;
import com.woniuxy.dto.DtoUserInfo;
import com.woniuxy.exception.UserException;
import com.woniuxy.parameter.UserLoginParam;
import com.woniuxy.service.PermissionService;
import com.woniuxy.service.UserService;
import com.woniuxy.shiro.JWTUtils;
import com.woniuxy.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authc.RememberMeAuthenticationToken;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/** 
*@author 王维鑫
*@Description 用户类操作，登录模块
*@Date 9:31 2021/5/13 0013
*@Param 
*@Return 
**/
@RestController
@RequestMapping("/user")
@CrossOrigin
@Api("用户登录")
public class UserController {
    /*业务层注入*/
    @Resource
    private UserService userService;
    /**
     *@Author 王维鑫
     *@Description
     *@Param [userLoginParam]
     *@return com.woniuxy.dto.JSONResult
     **/
    @ApiOperation("用户登录")
    @GetMapping("/login")
    public DtoJSONResult login(UserLoginParam userLoginParam)throws UserException {
            System.out.println("1"+userLoginParam.getAccount()+userLoginParam.getPassword());
            User u = userService.login(userLoginParam);
        if (null != u && u.getAccount().equals(userLoginParam.getAccount()) && u.getPassword().equals(userLoginParam.getPassword())) {
                //使用工具生成Token
                String token = JWTUtils.generatorJWT(u.getUserId().toString());

                return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), token, null);
            }
            //登陆失败抛异常
            throw new UserException(DtoUserEnum.PASSWORDERROR.getCode(), DtoUserEnum.PASSWORDERROR.getMessage());
        }
    @ApiOperation("第一次登录成功后跳转到主页后立马发请求查询用户信息")
    @GetMapping("/userInfo")
    public DtoJSONResult selectUserInfo(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        String id = JWTUtils.getId(token);
        User user = userService.getById(id);
        System.out.println("用户是："+user);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), user, null);
    }


}
